package com.df.tv.launcher.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import com.df.tv.launcher.R;

/**
 * 可以动态添加和溢出 Point item
 */
public class DynaPointsView extends LinearLayout {
    private ArrayList<ImageView> mPoints = new ArrayList<ImageView>();
    private int mPosition = -1;
    private int mOldPosition = -1;
    private Context mContext;
    private LinearLayout linearLayout;

    public DynaPointsView(Context context) {
        super(context);
        mContext = context;
        init(context);
    }

    public DynaPointsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray styles = context.obtainStyledAttributes(attrs, R.styleable.points);
        int pointNum = styles.getInt(R.styleable.points_num, 0);
        init(context);
        addNewPoints(context, pointNum);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mPointsView = inflater.inflate(R.layout.points, this);
        linearLayout = (LinearLayout) mPointsView.findViewById(R.id.layout_points);
    }

    public void addNewPoints(Context context, int addCount) {
        LinearLayout.LayoutParams params = null;
        for (int i = 0; i < addCount; i++) {
            if (i == 0) {
                params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER_VERTICAL;
                params.setMargins(2, 0, 2, 0);
            }
            ImageView imageView = new ImageView(context);
            imageView.setScaleType(ScaleType.CENTER);
            imageView.setImageResource(R.drawable.page_switch_point_unselected);
            linearLayout.addView(imageView, params);
            mPoints.add(imageView);
        }
    }

    /**
     * @return the mPoints
     */
    public ArrayList<ImageView> getPoints() {
        return mPoints;
    }

    public int getPosition() {
        return mPosition;
    }

    public void setPosition(int position) {
        switchPoint(position, mOldPosition);
    }

    /**
     * 切换到下一个
     */
    public void nextPosition() {
        int newP = (mOldPosition + 1) % mPoints.size();
        switchPoint(newP, mOldPosition);
    }

    private void switchPoint(int newPos, int oldPos) {
        if (oldPos >= 0 && oldPos < mPoints.size()) {
            mPoints.get(oldPos).setImageResource(R.drawable.page_switch_point_unselected);
        }
        if (newPos >= 0 && newPos < mPoints.size()) {
            mPoints.get(newPos).setImageResource(R.drawable.page_switch_point_selected);
        }
        mOldPosition = newPos;
    }

}
